'''
Created on Aug 12, 2009

@author: Roni
'''
import unittest
import cliques.cliqueSearchLowerbound
import tests.cliqueSearchAbstractTest

class test_lowerboundSearch(tests.cliqueSearchAbstractTest.CliqueSearchAbstractTest):
    
    def testBugGraph(self):
        GRAPH_FILE_NAME = "BUG-GRAPHOptimal[4,5].graph"
        randomAlg = cliques.randomCliqueSearch.randomCliqueSearch()
        lowerboundAlg = cliques.cliqueSearchLowerbound.CliqueSearchLowerbound()
        algorithms= [lowerboundAlg,randomAlg]
        test_graph = cliques.utils.import_graph(GRAPH_FILE_NAME)
        k=4
        start_node = u'5'
        comp_funcs=[self.assert_less_iterations,self.assert_at_least_k]
        self.simulate_full_run(test_graph, algorithms, comp_funcs, k, start_node)
    
    def testOptimality(self):
        '''
        Check that lowerbound is always a lower bound
        '''
        min_k=4
        max_k=8
        randomAlg = cliques.randomCliqueSearch.randomCliqueSearch()
        lowerboundAlg = cliques.cliqueSearchLowerbound.CliqueSearchLowerbound()
        algorithms= [lowerboundAlg,randomAlg]
        comp_funcs=[self.assert_less_iterations,self.assert_at_least_k]
        for nodes in xrange(10,200,10):
            self.clique_search_full_run_comparator(nodes, min_k, max_k, algorithms, comp_funcs)

    def assert_less_iterations(self,alg1,alg2):
        assert alg1.iteration<=alg2.iteration
    def assert_at_least_k(self,alg1,alg2):
        if alg1.done==True:
            assert alg1.iteration>=alg1.k-1
        if alg2.done==True:
            assert alg2.iteration>=alg2.k-1
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()